<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Site extends CI_Controller {

	private $tab = 'site';
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
		$admin = $this->session->userdata('admin_user');
		if(!$admin || !isset($admin['user_id'])){
			header('Location: /ychadmin/login');
			exit;
		}
	}

	function add_view(){
		$this->load->model('Site_model');
		$this->load->helper(array('form'));
		$vdata['site_array'] = $this->Site_model->get_site_select_array();

		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'site_article';

		$this->load->view("/ychadmin/site_add",$vdata);
	}

	function add(){
		$title 		= $this->input->post('title');
		$url 		= $this->input->post('url');
		$forder 	= $this->input->post('forder');
		$parent_id 	= $this->input->post('parent_id');
		$type_set 	= $this->input->post('type_set');
		$content 	= $this->input->post('content');

		$this->load->helper(array('form'));
		$this->load->library('form_validation');
		$this->form_validation->set_rules('title', 				'标题', 		'trim|min_length[1]|max_length[20]');
		$this->form_validation->set_rules('url', 				'URL', 		'required|trim|is_unique[ych_site.url]|min_length[1]|max_length[20]');
		$this->form_validation->set_rules('forder', 			'排序', 		'trim|integer|min_length[1]|max_length[20]');
		/*if($type_set !=1){
			$this->form_validation->set_rules('content', 			'内容', 		'required');
		}*/

		if ($this->form_validation->run() == FALSE){
   			$this->load->view('/ychadmin/site_add');
   			return;
  		}

  		$data['title'] 		= $title;
  		$data['url'] 		= $url;
  		$data['forder'] 	= $forder;
  		$data['parent_id'] 	= $parent_id;
  		$data['type_set'] 	= $type_set;
  		$data['content'] 	= $content;
  		$data['add_time'] 	= time();
  		$data['addip'] 	= $this->js->get_ip();
  		$this->load->model('Site_model');
  		$site_id = $this->Site_model->add($data);

  		header("location:/ychadmin/site/sitelist/");
		$this->load->model('log_model');$this->log_model->add_log('[添加][站点]['.$title.']');
   		exit;
	}

	function view($site_id){
		$this->load->model('Site_model');
  		$site = $this->Site_model->get_site_obj($site_id);
  		$vdata['site'] = $site;
  		$this->load->view("/ychadmin/site_view", $vdata);
	}

	function edit_view($site_id){
		$this->load->helper(array('form'));
		$this->load->model('Site_model');
  		$site = $this->Site_model->get_site_obj($site_id);
  		$vdata['site'] = $site;
  		$vdata['site_array'] = $this->Site_model->get_site_select_array();

  		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'site_article';

  		$this->load->view("/ychadmin/site_edit", $vdata);
	}

	function edit(){
		$id 		= $this->input->post('id');
		$title 		= $this->input->post('title');
		$url 		= $this->input->post('url');
		$forder 	= $this->input->post('forder');
		$type_set 	= $this->input->post('type_set');
		$parent_id 	= $this->input->post('parent_id');
		$summary 	= $this->input->post('summary');//最大为200字节
                $content 	= $this->input->post('content');

		$this->load->helper(array('form'));
		$this->load->library('form_validation');
		$this->form_validation->set_rules('id', 				'ID', 		'required');
		$this->form_validation->set_rules('title', 				'标题', 		'trim|min_length[1]|max_length[20]');
		$this->form_validation->set_rules('url', 				'URL', 		'required|trim|min_length[1]|max_length[20]');
		$this->form_validation->set_rules('forder', 			'排序', 		'trim|integer|min_length[1]|max_length[20]');

		/*if($type_set !=1){
			$this->form_validation->set_rules('content', 			'内容', 		'required');
		}*/

		if ($this->form_validation->run() == FALSE){
   			$this->load->view('/ychadmin/site_edit');
   			return;
  		}

  		$data['title'] = $title;
  		$data['url'] = $url;
  		$data['forder'] = $forder;
  		$data['type_set'] = $type_set;
  		$data['parent_id'] = $parent_id;
        $data['summary'] = $summary;
  		$data['content'] = $content;
  		$this->load->model('Site_model');
  		$site_id = $this->Site_model->update($id, $data);

  		//header("location:/ychadmin/site/edit_view/".$id);
        header("location:/ychadmin/site/sitelist/");
		$this->load->model('log_model');$this->log_model->add_log('[修改][站点]['.$title.']'.'ID=='.$id);
   		exit;
	}

	function sitelist(){
		//接收参数
		$per_page 			= $this->input->get('per_page')?$this->input->get('per_page'):0;
		$pid 			= $this->input->get('parent_id');

		$data = array();

		if($pid != ''){
			$data['where']['parent_id'] = $pid;
		}


		$data['page_size'] = 10;
		$data['page_start'] = $per_page;

		$this->load->model('Site_model');
		$vdata = $this->Site_model->get_site_category_list($data);

		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/ychadmin/site/sitelist/?title='.$title.'&fgroup='.$fgroup;
		$config['total_rows'] = $vdata['site_count'];
		$config['per_page'] = YTZ_LIST_PAGE_SIZE;
		$this->pagination->initialize($config);

		$vdata['title'] 	= $title;
		$vdata['pid'] 	= $pid;
		$vdata['url'] 		= $url;
		//$vdata['group_list']= $this->Site_model->get_fgroup();
		$vdata['site_array'] = $this->Site_model->get_site_select_parent_array();

		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'site_article';

		$this->load->view('/ychadmin/site_list', $vdata);
	}

	function remove(){
		$id 				= $this->input->post('id');
  		$data["is_del"]		= 1;
  		$this->load->model('Site_model');
  		$this->load->model('log_model');$this->log_model->add_log('[删除][站点]');
		echo  $this->Site_model->update($id, $data);
	}

}
